package org.web.schedule;

import java.util.Date;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import javax.annotation.Resource;

import org.net.util.AnalysisContentUtil;
import org.net.util.FetchUtil;
import org.net.util.StringUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.web.core.service.CommentResourcesService;
import org.web.core.service.ResourceService;
import org.web.entity.CommentResources;
import org.web.entity.Resources;

/**
 * @Author : yingx
 * @CreateTime : 2011-10-10 下午01:51:51
 * @Pageage : org.web.schedule
 */
public class CallAutoExecuteFetchSiteJob {
	
	@Resource(name = "resourceService")
    private ResourceService resourceService;
	
	@Resource(name = "commentResourcesService")
    private CommentResourcesService commentResourcesService;
	
	private static Logger log = LoggerFactory.getLogger(CallAutoExecuteFetchSiteJob.class);
	
	private static final int page_size = 10;
	
	private static HashSet<String> ids = new HashSet<String>();
    
    public static HashSet<String> getIds() {
		return ids;
	}

	@SuppressWarnings("unchecked")
	public void execute() {
    	log.info("crawl comment resource quartz schedule is starting.....");
    	Map<String, Object> parameters =new HashMap();
    	parameters.put("fecthSource","yes");
    	parameters.put("limit", page_size);
    	parameters.put("offset", 0);
    	if(!ids.isEmpty()){    //避免重复
    	  String curids = "";
    	  Iterator itor = ids.iterator();
    	  while(itor.hasNext()){
    		  curids = (String)itor.next()+",";
    	  }
    	  if(!StringUtil.isEmpty(curids))     curids = curids.substring(0,curids.length()-1);
    	  parameters.put("ids", curids);
    	}
    	if(ids.size()<8){
    	List<Resources> sources = resourceService.getResourcesListByConditions(parameters);
    	for(Resources rs : sources){
    	   ids.add(rs.getId().toString());
    	   final Resources res = rs;
    	   new Thread(){
    	    public void run(){	
    	    try{
    		  long start = System.currentTimeMillis();
			  String targetUrl ="http://siteexplorer.search.yahoo.com/search?p=www."+AnalysisContentUtil.getItsDomainName(res.getUrl())+"&fr=sfp&bwm=i";
			  String responseBody = FetchUtil.getHTMLResponseBodyAsString(targetUrl);
			  if(!StringUtil.isEmpty(responseBody)){
				Pattern p = null;
				Matcher m = null;
				StringBuffer sb0 = new StringBuffer();
				String regex ="<span class=\"result\"><a.*href=\"(.*)\".*>(.+?)</a>(.*)<p class=\"details\">(.*)</p><p class=\"info\">(.*)<span class=\"size\">(.*)</span></p></span>";
				p = Pattern.compile(regex);
				m = p.matcher(responseBody);
				while (m.find()) {
					sb0.append(m.group());
				}
				//所有前100条链接
				List<String> sites = AnalysisContentUtil.fetchAllHrefFromYahooSite(sb0.toString());
				int foreignLinkCount =0;
				for(String st:sites){
					try{
					//本身的内链及网页的主页等
					if(AnalysisContentUtil.getItsDomainName(res.getUrl()).equalsIgnoreCase(AnalysisContentUtil.getItsDomainName(st))){
						continue;
					}
					if(!StringUtil.isEmpty(st)){
						String abc = st.replaceAll("http://", "");
						if(abc.indexOf("/")!=-1){  //去除最后的"/"
							abc = abc.substring(0,abc.lastIndexOf("/"));
						}
						if(abc.indexOf("/")==-1 && abc.indexOf("?")==-1){
							continue;
						}
					}
					//打开页面并判断页面上所存在的链接数
					foreignLinkCount = AnalysisContentUtil.countAllHrefExceptSelfInputSteam(st,resourceService);
					if(foreignLinkCount>0 && foreignLinkCount<1000){ // 当前页面不是本站的链接数量
						String html = FetchUtil.getHTMLResponseBodyAsString(st);
					    if(AnalysisContentUtil.canLeaveComment(html.toString())){ //是否有留言标识
					      log.info("find leave comment："+st+"\r\n");
					      this.currentThread().sleep(100);
						  CommentResources cr = new CommentResources();
						  cr.setCreatedDate(new Date());
						  cr.setFromWhere("link:www."+AnalysisContentUtil.getItsDomainName(res.getUrl()));
						  cr.setIsUsed("N");
						  cr.setUrl(st);
						  commentResourcesService.save(cr);
					    }
					}
					}catch(Exception e) { e.printStackTrace(); continue; }
				}
			}
				
		    res.setIsCrawled("Y");
			res.setUpdatedDate(new Date());
			resourceService.update(res);
			ids.remove(res.getId().toString());
			long end = System.currentTimeMillis();
			log.info("crawl "+res.getUrl()+" cost times："+(end-start));
    	 }
   		 catch(Exception e) {
   			 e.printStackTrace();
   			 res.setIsCrawled("E");   //将不再出现在抓取列表中,视无效资源
  			 resourceService.update(res);
   		 }
    	}
    	}.start();
        }
    	}else{
    		log.info("crawl comment resource number rather than 10,no need to start another one");
    	}
    	log.info("crawl comment resource quartz schedule is end");
    }
}
